package org.NooLab.randomgraph.components;

import java.util.ArrayList;

import org.NooLab.randomgraph.RandomGraphIntf;
import org.NooLab.randomgraph.properties.RGraphProperties;
import org.NooLab.structures.randomgraph.ParticledText;
import org.NooLab.structures.randomgraph.RawEntityPosition;
import org.NooLab.structures.randomgraph.RawEntityPositions;
import org.NooLab.structures.randomgraph.WordNeighborhood;
import org.NooLab.structures.randomgraph.WordPosition;
import org.NooLab.structures.randomgraph.WordPositions;
import org.NooLab.utilities.logging.PrintLog;



/**
 * 
 * 
 * 
 *
 */
public class TopologyProcessor {

	RGraphProperties rgProperties;
	RandomGraphIntf randomGraph;
	// PluggedCapabilities pCap;
	
	int activeEntity=0;
	
	// ========================================================================
	public TopologyProcessor(RandomGraphIIntf rGraph, int activeentity) {
		randomGraph = rGraph;
		rgProperties =  rGraph.getRgProperties() ;
		// pCap = rgProperties.getPluggedCapabilities() ;
		
		activeEntity = activeentity ;
		
	}
	// ========================================================================

 

	/**
	 * here, we create the neighborhoods, which consider 
	 *  - sentence borders, sentence properties (if available),
	 *    such as "?" "!", or position of "," "-" 
	 * ...according to settings
	 * 
	 * Sentence stuff is provided by the 
	 * 
	 *  -> we need a property vector (type,weight) that indicates whether a word
	 *     is from another sentence
	 *  
	 *   Alternatively we could add a NIL value
	 * 
	 */
	public void createNeighborhoods(ParticledText pText) {

		RawEntityPositions rwps;
		WordPositions cwps ;  
		WordPosition cwp;
		WordNeighborhood wordNbHood;
		ArrayList<Integer> ps;
		RawEntityPositions wordlist;
		RawEntityPosition rawWord ;
		
		int lowerPosConstraint,upperPosConstraint;
		int wordpos , npos,wordIndex;
		int nbSize = 5, nbSize2=2, asymmCenterShift=0 ; // dependent on properties and capabilities
		String wordStr="", wstr="";
		
		if (rgProperties.getLengthNeighborhood()>=3){
			nbSize = rgProperties.getLengthNeighborhood() ;
			nbSize2 = (int)nbSize/2;
		}
		
		try{
		
			rwps = pText.getWordPositions() ;
			cwps = pText.getCollWordPositions() ; // this contains : ArrayList<WordNeighborhood> neighborhoods
			
			// we need to build collections of contexts per unique word
			// -> if a word occurs 3 times, we build three neighborhoods

			// around this word we need the neighborhood... visiting the ordinary word list
			wordlist = pText.getWordPositions();
			 
			lowerPosConstraint = 0;
			upperPosConstraint = wordlist.size();
			if (rgProperties.getAvoidNILinContexts()){
				lowerPosConstraint = nbSize2+1;
				upperPosConstraint = wordlist.size()-nbSize2-1;
			}
			                                                  
			                                      /*
			                                       *TODO  make it parallel with collectors thread
			                                       */
			
			 												PrintLog.Print(2, "building contexts (TopologyProcessor)... ");
			for (int i=0;i<cwps.size();i++){
				// word i
				cwp = cwps.getItem(i) ;
				String cwordStr = cwp.getWordStr() ;
															PrintLog.printPrc(2,i, cwps.size(), cwps.size()/10," ");

				ps = cwp.getPositions() ;
				if (ps.size()==0){
					cwp.setStatus(-3); continue;} // exclude delimiters !!!  .,:!
if (i==23){
	npos=0;
}
				for (int p=0;p<ps.size();p++){
				
					wordNbHood = new WordNeighborhood(rgProperties);
					wordpos = ps.get(p) ;

					if ( wordpos > wordlist.size()-1){
						continue;
					}
					rawWord = wordlist.getItem(wordpos) ;
					wordStr = rawWord.getTextualStr() ;		

					wordNbHood.setCenterWordPos(wordpos) ;
					wordNbHood.setCenterWordStr(wordStr) ;
					
					
					asymmCenterShift = 0 ;
					
					
					for (int n=0;n<nbSize;n++){
					
						npos = -3;
						npos = wordpos - nbSize2 + n;
						
						if ((npos>=lowerPosConstraint) && (npos<upperPosConstraint )){
							// wstr = rawWord.getTextualStr() ;
							wstr = wordlist.getItem(npos).getTextualStr() ;
								// pText.getWords().get(npos) ;	
						}else{
							wstr = RandomGraphProcessorIntf._RG_MARKER_NIL ;
							npos = RandomGraphProcessorIntf._RG_MARKERID_NIL ;
						}
						if (wstr.length()>0){
							wordIndex = npos; 
							wordNbHood.getWordIndices()[n]   = npos;
							wordNbHood.getWordNeighbors()[n] = wstr ;
						}else{
							wordNbHood.getWordIndices()[n]   = RandomGraphProcessorIntf._RG_MARKERID_NIL ;
							wordNbHood.getWordNeighbors()[n] = RandomGraphProcessorIntf._RG_MARKER_NIL;
						}
						
						
					}// n->nbSize
					
					if (wordNbHood!=null){
						cwp.getNeighborhoods().add(wordNbHood) ;
					}
					
				} // p-> all positions in raw text
			
				wstr=wstr+" " ;
			} // i-> cwps
			
			wstr=wstr+" " ;
			
			
		}catch(Exception e){
			e.printStackTrace() ;
		}
		wstr=wstr+" " ;
		
	}




	public RGraphNeighborhood getNeighborhoods() {
		// TODO Auto-generated method stub
		return null;
	}
}
